﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Bussiness.Service;
using CrystalDecisions.CrystalReports.Engine;
using Ext.Net;
using System.Data;
using System.Collections;

namespace Bussiness.View.BackEnd
{
    public partial class BC_HangTon : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadData();
            
                string action = Request.Params.Get("Action");
                if (action == "print" && !Formater.isNull(action))
                {
                    In_BK();
                }
            
        }
        public void LoadData()
        {
            List<Product> list = new Product_Service().getAllStore();
            List<SizeProduct> listSize;
            decimal thanhtien = 0;
            for (int i = 0; i < list.Count; i++)
            {
                listSize = new Product_Service().getListSizeByProductID(list[i].ID);
                if (listSize.Count != 0)
                {
                for (int j = 0; j < listSize.Count; j++)
                {
                    if (j != (listSize.Count - 1))
                        list[i].ListSize += listSize[j].Size + ",";
                    else
                        list[i].ListSize += listSize[j].Size;
                }

                }

                thanhtien += (decimal)(list[i].PriceImport * list[i].Count);
            }
            this.giaTriThanhTien.Text = Utils.FormatMoney( Convert.ToInt32(thanhtien).ToString()) + " VNĐ";
            DSHangHoa_Store.DataSource = list;
            DSHangHoa_Store.DataBind();
        }

        public void In_BK()
        {

            DataTable table = new Product_Service().getBKSP();
            ReportDocument crystalReport = new ReportDocument();
            crystalReport.Load(Server.MapPath("Report/TK_HangTon.rpt"));
            
            foreach (System.Data.DataRow dr in table.Rows)
            {
                List<SizeProduct> listSize = new Product_Service().getListSizeByProductID(int.Parse(dr["id"].ToString()));
                dr["description"] = "";
                for (int i = 0; i < listSize.Count;i++ )
                {
                    if (i < listSize.Count - 1)
                        dr["description"] += listSize[i].Size + ",";
                    else
                        dr["description"] += listSize[i].Size;
                }
            }
            crystalReport.SetDataSource(table);
            // truyen tham so ngay
            string currDate = "Ngày " + DateTime.Now.Day.ToString() + " tháng " + DateTime.Now.Month.ToString() + " năm ";
            crystalReport.SetParameterValue("ngayTK", currDate);

            this.CrystalReportViewer1.ReportSource = crystalReport;
            CrystalReportViewer1.DataBind();



            this.TK_HangTon.Hidden = false;
        }
    }
}
